// pages/pinglun/pinglun.js
var app = getApp();
var utils = require('../../utils/shop/formatShop.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    front_url: app.globalData.init.front_url,
    Order_Type: 'shop',
    Order_ID: 0,  //显示的订单id

    Score: 0, //评分

    order_detail: {},

    upload_img_path: [],
    Commit_Image: [],  //上传的图片
  },

  //评论提交
  commit: function (e) {
    if (!this.data.Score) {
      wx.showModal({
        title: '提示',
        content: '请选择评分',
        showCancel: false
      });
      return false;
    }

    var Note = e.detail.value.Note;
    if (Note == '') {
      wx.showModal({
        title: '提示',
        content: '请填写评论内容',
        showCancel: false
      });
      return false;
    }

    var commitArgs = {
      //uri: '/api/' + app.globalData.userInfo.Users_ID + '/shop/member/ajax/',
      action: 'commit',
      OrderID: this.data.Order_ID,
      User_ID: app.globalData.userInfo.User_ID,
      UsersID: app.globalData.userInfo.Users_ID,
      Score: this.data.Score,
      Note: Note,
      type: 'confirm'
    };
    app.http_req(commitArgs, app.globalData.init.api_url, 'POST', this.ordercommit);
  },

  //评论提交回调
  ordercommit: function (res) {
    if (res.errorCode == 0 || res.status == 1) {
      // 更新订单页该订单的评论状态
      var pages = getCurrentPages();          //获取来源页面堆栈
      var currPage = pages[pages.length - 1];   //当前页面
      var prevPage = pages[pages.length - 2];  //上一个页面

      var orderlist = prevPage.data.orderlist;
      for (var i in orderlist) {
        if (orderlist[i].Order_ID == this.data.Order_ID) {
          orderlist[i].Is_Commit = 1;
        }
      }
      prevPage.setData({
        orderlist: orderlist
      });

      wx.showToast({
        title: '评论成功',
        icon: 'success',
        duration: 1000,
        success: function () {
          setTimeout(function () {
            //要延时执行的代码
            wx.navigateBack({
              delta: 1
            });
          }, 1000) //延迟时间 
        }
      });
    } else {
      wx.showModal({
        title: '提示',
        content: typeof res.msg != 'undefined' ? res.msg : '提交失败，请稍后重试！',
        showCancel: false
      })
    }
  },

  //上传图片
  uploadFile: function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      success: function (res) {
        var image = res.tempFilePaths[0];
        var formData = {
          act: 'upload_image',
          uri: '/api/little_program/shopconfig.php',
          appid: app.globalData.init.appid,
          Users_Account: app.globalData.init.Users_Account,
          User_ID: app.globalData.userInfo.User_ID
        }
        //添加签名
        formData = app.createToken(formData);
        //上传
        wx.uploadFile({
          url: app.globalData.init.api_url,
          filePath: image,
          name: 'image',
          formData: formData,
          success: function (res2) {
            var callback_res = JSON.parse(res2.data);
            if (callback_res.errorCode == 0) {
              var upload_img_path = that.data.upload_img_path;
              var Commit_Image = that.data.Commit_Image;
              upload_img_path.push(image);
              Commit_Image.push(callback_res.path);

              that.setData({
                upload_img_path: upload_img_path,
                Commit_Image: Commit_Image
              });
            } else {
              wx.showModal({
                title: '提示',
                content: typeof callback_res.msg != 'undefined' ? callback_res.msg : '上传失败，请稍后重试',
                showCancel: false
              });
            }
          }
        });
      },
    })
  },

  //上传图片点击预览
  proImgPreview: function (e) {
    var index = e.currentTarget.dataset.index;
    var ImgPath = this.data.upload_img_path;
    //处理缩略，去除nx
    for (var i in ImgPath) {
      ImgPath[i] = ImgPath[i].replace(/n\d\//, '');
    }
    wx.previewImage({
      current: ImgPath[index],
      urls: ImgPath
    });
  },

  //删除上传图片
  delImg: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var upload_img_path = that.data.upload_img_path;
    var Commit_Image = this.data.Commit_Image;
    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: function (res) {
        if (res.confirm) {
          upload_img_path.splice(index, 1);
          Commit_Image.splice(index, 1);
          that.setData({
            upload_img_path: upload_img_path,
            Commit_Image: Commit_Image
          });
        }
      }
    });
  },

  //评分选择
  score_choose: function (e) {
    var Score = e.currentTarget.dataset.score;
    this.setData({
      Score: Score
    });
  },

  //获取订单详情回调
  getorders: function (res) {
    if (res.errorCode == 0) {
      var order_list = utils.formatOrder(app.globalData.init.front_url, [res.data[0]]);
      this.setData({
        order_detail: order_list[0]
      });
      if (order_list[0].Order_Status != 4 || order_list[0].Is_Backup != 0) {
        wx.showModal({
          title: '提示',
          content: '该订单不是待评论订单，不能评论',
          showCancel: false,
          success: function () {
             wx.navigateBack({
               delta: 1
             });
          }
        });
        return false;
      }
      if (order_list[0].Is_Commit != 0) {
        wx.showModal({
          title: '提示',
          content: '该订单已评论过',
          showCancel: false,
          success: function () {
            wx.navigateBack({
              delta: 1
            });
          }
        });
        return false;
      }

     
    } else {
      wx.showModal({
        title: '提示',
        content: '获取订单详情失败，请稍后重试！',
        showCancel: false,
        success: function () {
          wx.navigateBack({
            delta: 1
          });
        }
      });
    }
  },

  //页面加载
  load: function () {
    //检查用户是否登录
    if (!app.globalData.userInfo) {
      app.login('', this.load);
      return false;
    }

    //请求订单详情
    var orderArgs = {
      act: 'get_order',
      User_ID: app.globalData.userInfo.User_ID,
      Order_ID: this.data.Order_ID,
      Order_Type: this.data.Order_Type,
    };
    app.http_req(orderArgs, app.globalData.init.api_url, 'POST', this.getorders);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.Order_Type) {
      this.setData({
        Order_Type: options.Order_Type
      });
    }
    if (options.Order_ID) {
      this.setData({
        Order_ID: options.Order_ID
      });
      this.load();
    } else {
      wx.showModal({
        title: '提示',
        content: '缺少必要参数',
        showCancel: false,
        success: function () {
          wx.navigateBack({
            delta: 1
          });
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})